package com.ken.android.music.view.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;

import androidx.annotation.Nullable;

import com.ken.android.music.mvp.B.BasePresenter;
import com.ken.android.music.mvp.B.I.IBaseView;
import com.ken.android.music.service.PlayerService;

public abstract class BaseAudioActivity<V extends IBaseView, P extends BasePresenter<V>> extends BaseActivity<V,P>{

    protected PlayerService player;
    private MediaControllerCompat controllerCompat;
    private MediaBrowserCompat browserCompat;
    private BaseAudioActivity activity = null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        activity = this;
        super.onCreate(savedInstanceState);
//        bindAudioService();
        initMediaBrowser();
    }

    @Override
    protected void onStart() {
        super.onStart();
        browserCompat.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        // (see "stay in sync with the MediaSession")
        if (MediaControllerCompat.getMediaController(this) != null) {
            MediaControllerCompat.getMediaController(this).unregisterCallback(controllerCallback);
        }
        browserCompat.disconnect();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        unbindAudio();
        activity = null;
    }

    private void bindAudioService(){
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(this, PlayerService.class));
        startService(intent);
        bindService(intent,connection,BIND_AUTO_CREATE);
    }

    protected void unbindAudio(){
        unbindService(connection);
    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            player = (PlayerService) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            player = null;
        }
    };

    protected PlayerService getPlayer(){
        return player;
    }

    private MediaControllerCompat.Callback controllerCallback = new MediaControllerCompat.Callback() {
        @Override
        public void onPlaybackStateChanged(PlaybackStateCompat state) {
            super.onPlaybackStateChanged(state);
        }
    };

    private void initMediaBrowser() {
        browserCompat = new MediaBrowserCompat(this,
                new ComponentName(this, PlayerService.class),
                connectionCallback,
                null); // optional Bundle
    }

    private MediaBrowserCompat.ConnectionCallback connectionCallback  = new MediaBrowserCompat.ConnectionCallback(){
        @Override
        public void onConnected() {
            super.onConnected();
            MediaSessionCompat.Token token = browserCompat.getSessionToken();
            try {
                controllerCompat = new MediaControllerCompat(activity, // Context
                        token);
            } catch (RemoteException e) {
                e.printStackTrace();
            }

            // Save the controller
            MediaControllerCompat.setMediaController(activity, controllerCompat);

            // Finish building the UI
        }
    };

    public PlaybackStateCompat getPlaybackState(){
        return controllerCompat != null?controllerCompat.getPlaybackState():null;
    }

    public MediaControllerCompat.TransportControls getTransports(){
        return controllerCompat != null?controllerCompat.getTransportControls():null;
    }

    public MediaControllerCompat getControllerCompat() {
        return controllerCompat;
    }
}
